<?php
/**
 * 新闻表 包含所有新闻的内容
 *@author wolf  [Email: 116311316@qq.com]
 *@since 2011-07-20
 *@version 1.0
 */
class NewsModel extends Db {
	/**
	 * 新闻分类表
	 *
	 * @var string
	 */
	protected $_news_category = 'w_news_category';
	/**
	 * 新闻列表
	 *
	 * @var string
	 */
	protected $_news_list = 'w_news_list';
	/**
	 * 新闻内容表
	 *
	 * @var string
	 */
	protected $_news_content = 'w_news_content';
	protected $_news_image = 'w_news_image';
	protected $_news_special = 'w_news_special';
	/**
	 * 新闻评论表
	 *
	 * @var table
	 */
	protected $_news_comment = 'w_news_comment';
	protected $_templates = 'w_templates';
	protected $_extend = 'w_extend';
	protected $_extend_value = 'w_extend_value';
	protected $_flag = 'w_flag';
	protected $_cache = 'w_cache';
	protected $_cate_extend = 'w_category_extend';
	protected $_templates_type = "w_templates_type";
	/**
	 * 新增3张扩展表
	 */
	protected $_extend_int = 'w_extend_int';
	protected $_extend_decimal = 'w_extend_decimal';
	protected $_extend_varchar = 'w_extend_varchar';
	
	protected $_comment_reply = 'w_comment_reply';
	/**
	 * 多语言
	 * Enter description here ...
	 * @var unknown_type
	 */
	protected $_lang = 'w_lang';
	/**
	 * 添加历史记录
	 *
	 * @var unknown_type
	 */
	protected $_templates_history = 'w_templates_history';
	/**
	 * 查看评论
	 *
	 * @param array $where
	 * contentId
	 */
	public function getComment($where = NULL, $num = NULL) {
		if (empty ( $where )) {
			$sql = "SELECT a.title,b.* FROM $this->_news_list a ,$this->_news_comment b WHERE a.id=b.nid  ORDER BY b.id DESC";
			return $rs = $this->fetchAll ( $sql );
		} else {
			return $rs = $this->getAll ( $this->_news_comment, $where, null, 'id DESC', $num );
		}
	}
	/**
	 * 获取语言
	 * Enter description here ...
	 * @param unknown_type $where
	 */
	public function getLang($where) {
		return $this->getAll ( $this->_lang, $where );
	}
	/**
	 * 获取主题模板
	 * Enter description here ...
	 * @param unknown_type $where
	 */
	public function getTemplatesType($where = NULL) {
		return $this->getAll ( $this->_templates_type, $where );
	}
	/**
	 * 获取最近100条评论
	 * Enter description here ...
	 */
	public function getCommentNoRead() {
		$sql = "select count(id) num from (SELECT id,status FROM w_news_comment  where nid!=32416 order by id DESC limit 50) a where a.status=0";
		// $sql = "SELECT id FROM $this->_news_comment WHERE status=0 ORDER BY id DESC LIMIT 50";
		return $this->fetch ( $sql );
	}
	/**
	 * 查看评论
	 *
	 * @param array $where
	 * contentId
	 */
	public function getNewComment($ids, $num) {
		$sql = "SELECT a.title,b.* FROM $this->_news_list a ,$this->_news_comment b WHERE a.id=b.nid AND b.status=1 ORDER BY b.id DESC LIMIT $num";
		return $rs = $this->fetchAll ( $sql );
	}
	/**
	 *
	 *
	 *
	 *
	 * 有目录
	 *
	 * @param array $params        	
	 */
	public function addCache($params) {
		return $this->add ( $this->_cache, $params );
	}
	/**
	 * 获取字段
	 * Enter description here .
	 *
	 *
	 *
	 *
	 * @param unknown_type $where        	
	 */
	public function getCacheByCid($where) {
		return $this->getOne ( $this->_cache, $where );
	}
	/**
	 * 清空统计结果
	 * Enter description here .
	 *
	 *
	 *
	 *
	 * ..
	 */
	public function clearNumCache() {
		$sql = "TRUNCATE $this->_cache";
		$this->exec ( $sql );
	}
	/**
	 * 删除多余的扩展字段
	 *
	 * @param unknown_type $type        	
	 * @param unknown_type $key        	
	 */
	public function DeleteExtendDecimal() {
		$sql = "DELETE FROM $this->_extend_decimal WHERE `value`=NULL";
		return $this->exec ( $sql );
	}
	public function DeleteExtendInt() {
		$sql = "DELETE FROM $this->_extend_int WHERE `value`=NULL";
		return $this->exec ( $sql );
	}
	public function DeleteExtendVarchar() {
		$sql = "DELETE FROM $this->_extend_varchar WHERE `value`=NULL";
		return $this->exec ( $sql );
	}
	/**
	 * 查看评论
	 *
	 * @param array $where
	 * contentId
	 */
	public function commentPage($start, $end, $where) {
		if (empty ( $where )) {
			$sql = "SELECT a.title,b.* FROM $this->_news_list a ,$this->_news_comment b WHERE a.id=b.nid AND b.nid!=32416 ORDER BY b.id DESC LIMIT $start,$end";
		} else {
			$sql = "SELECT a.title,b.* FROM $this->_news_list a ,$this->_news_comment b WHERE a.id=b.nid AND b.nid=$where[nid] ORDER BY b.id DESC LIMIT $start,$end";
		}
		$rs = $this->fetchAll ( $sql );
		// $rs = $this->getAll($this->_news_comment, $where, null, 'id DESC',
		// $num);
		return $rs;
	}
	/**
	 * 添加flag
	 *
	 * @param unknown_type $params        	
	 */
	public function addFlag($params) {
		$this->add ( $this->_flag, $params );
		/* 检查是否存在该范围内 */
		$sql = "ALTER TABLE `w_news_list` CHANGE `flag` `flag` SET('1','2','3','4','5','6','7','8','9','10') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
        ";
	}
	/**
	 * 获取分类扩展字段
	 *
	 * @param
	 * int id
	 * @return array
	 */
	public function getCateExtend($cateid) {
		$sql = "select * from w_extend a LEFT JOIN w_category_extend b ON a.eid=b.eid WHERE b.cid IN ($cateid) group by a.eid";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 自定义标签
	 * Enter description here .
	 */
	public function getFlag($where = NULL) {
		return $this->getAll ( $this->_flag, $where );
	}
	/**
	 * 添加模板
	 *
	 * @param unknown_type $params        	
	 */
	public function addTemp($params) {
		$this->add ( $this->_templates, $params );
	}
	/**
	 * 获取制定模板
	 *
	 * @param unknown_type $name        	
	 */
	public function getTemplateList($start, $num, $key = null, $where = null) {
		return $this->getPage ( $start, $num, $this->_templates, $key, $where );
	}
	public function getTempHistory($where, $key, $limit = 20) {
		return $this->getAll ( $this->_templates_history, $where, $key, 'id DESC', $limit );
	
	}
	public function deleteTempHistory($temp_id, $id) {
		$sql = "DELETE FROM $this->_templates_history WHERE temp_id=$temp_id AND id<$id";
		$this->exec ( $sql );
	}
	/**
	 * 根据id 获取模板内容
	 *
	 * @param int $id        	
	 */
	public function getTemplateById($id) {
		return $this->getOne ( $this->_templates, array ('id' => $id ) );
	}
	/**
	 * 增加字段
	 * Enter description here .
	 *
	 *
	 *
	 *
	 *
	 * @param unknown_type $params        	
	 */
	public function addExtend($params) {
		$this->add ( $this->_extend, $params );
	}
	/**
	 * 获取字段名
	 *
	 * @param unknown_type $where        	
	 */
	public function getExtend($where = NULL) {
		return $this->getAll ( $this->_extend, $where );
	}
	/**
	 * 获取扩展字段的值 左外连接
	 * Enter description here .
	 *
	 *
	 *
	 * @param unknown_type $gid        	
	 */
	public function getExtendByGid($gid, $eids = null) {
		if ($eids == null) {
			$sql = "select a.status,a.type,a.name,a.key,a.num,b.id,b.varchar,b.decimal,b.int,b.modified from w_extend a left outer join w_extend_value b on a.key=b.key AND b.gid=$gid where a.module='news'";
		} else {
			$sql = "select a.status,a.type,a.name,a.key,a.num,b.id,b.varchar,b.decimal,b.int,b.modified from w_extend a left outer join w_extend_value b on a.key=b.key AND b.gid=$gid where a.module='news' AND a.eid IN ($eids)";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取字段值
	 *
	 * @param unknown_type $where        	
	 */
	public function getExtendValue($where, $key = NULL) {
		// $sql="SELECT $this->_extend WHERE FIND_IN_SET('news','module')";
		// return $this->fetchAll($sql);
		return $this->getAll ( $this->_extend_varchar, $where, $key );
	}
	/**
	 * 增加扩展字段的值
	 *
	 * @param unknown_type $params        	
	 */
	public function addExtendValue($params) {
		$this->add ( $this->_extend_value, $params );
	}
	public function addExtendInt($params) {
		$this->add ( $this->_extend_int, $params );
	}
	public function addExtendVarchar($params) {
		$this->add ( $this->_extend_varchar, $params );
	}
	public function addExtendDecimal($params) {
		$this->add ( $this->_extend_decimal, $params );
	}
	//
	public function getExtendInt($gid, $ids) {
		$sql = "SELECT * FROM $this->_extend_int a LEFT JOIN $this->_extend b ON a.eid=b.eid WHERE a.eid IN ($ids) AND gid=$gid ";
		return $this->fetchAll ( $sql );
	}
	public function getExtendVarchar($gid, $ids) {
		$sql = "SELECT * FROM $this->_extend_varchar a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE a.eid IN ($ids) AND gid=$gid ";
		return $this->fetchAll ( $sql );
	}
	public function getExtendDecimal($gid, $ids) {
		$sql = "SELECT * FROM $this->_extend_decimal a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE a.eid IN ($ids) AND gid=$gid ";
		return $this->fetchAll ( $sql );
	}
	public function getExtendIntByWhere($gid, $moduleid, $key = NULL) {
		$sql = "SELECT * FROM $this->_extend_int a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid";
		if (! empty ( $key )) {
			$sql = "SELECT * FROM $this->_extend_int a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid AND b.key='$key'";
		}
		return $this->fetchAll ( $sql );
	}
	public function getExtendVarcharByWhere($gid, $moduleid, $key = NULL) {
		$sql = "SELECT * FROM $this->_extend_varchar a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid";
		if (! empty ( $key )) {
			$sql = "SELECT * FROM $this->_extend_varchar a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid AND b.key='$key'";
		}
		return $this->fetchAll ( $sql );
	}
	public function getExtendDecimalByWhere($gid, $moduleid, $key = NULL) {
		$sql = "SELECT * FROM $this->_extend_decimal a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid ";
		if (! empty ( $key )) {
			$sql = "SELECT * FROM $this->_extend_decimal a LEFT JOIN $this->_extend b ON a.eid=b.eid  WHERE gid=$gid AND a.moduleid=$moduleid AND b.key='$key'";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 *
	 * @param unknown_type $v        	
	 * @param unknown_type $where        	
	 */
	public function saveExtendValue($v, $where) {
		$this->update ( $this->_extend_value, $v, $where );
	}
	public function saveExtendVarchar($v, $where) {
		$this->update ( $this->_extend_varchar, $v, $where );
	}
	public function saveExtendDecimal($v, $where) {
		$this->update ( $this->_extend_decimal, $v, $where );
	}
	public function saveExtendInt($v, $where) {
		$this->update ( $this->_extend_int, $v, $where );
	}
	public function increaseExtendValue($key, $gid) {
		$sql = "UPDATE w_extend_value SET `int`=`int`+1 WHERE gid=$gid and `key`='$key'";
		// $sql = "UPDATE $this->_extend_value SET $key=$key+1 WHERE gid=$gid";
		return $this->exec ( $sql );
	}
	/**
	 * 删除扩展字段
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $where        	
	 */
	public function delExtendValue($where) {
		$this->delete ( $this->_extend_value, $where );
	}
	/**
	 * 设置模板状态
	 *
	 * @param int $id        	
	 * @param int $status        	
	 */
	public function setTempldateStatus($id, $v) {
		return $this->update ( $this->_templates, $v, array ('id' => $id ) );
	}
	public function copyNews($id, $category) {
		$sql = "INSERT INTO $this->_news_list (title,cid,author,date,thumb,pic,views,summary) SELECT title,$category,author,date,thumb,pic,views,summary FROM $this->_news_list WHERE id=$id;";
		$this->exec ( $sql );
		$nid = $this->lastInsertId ();
		$sql1 = "INSERT INTO $this->_news_content (nid,content) SELECT $nid,content FROM $this->_news_content WHERE nid=$id;";
		return $this->exec ( $sql1 );
	}
	/**
	 * 添加模板历史记录
	 *
	 * @param unknown_type $params        	
	 */
	public function addTempHistory($params) {
		return $this->add ( $this->_templates_history, $params );
	}
	/**
	 *
	 * @param unknown_type $name        	
	 */
	public function getTemplateByName($name) {
		return $this->getOne ( $this->_templates, array ('name' => $name ) );
	}
	/**
	 * 更新属性
	 */
	public function setNewsSort($v, $id) {
		$sql = "UPDATE w_news_list SET `sort`='$v' WHERE `id`='$id'";
		return $this->exec ( $sql );
	
		// $this->update ( $this->_news_list, $v, $where );
	}
	/**
	 * 添加评论
	 *
	 * @param array $arr
	 * 内容
	 */
	public function addComment($arr) {
		$this->add ( $this->_news_comment, $arr );
		return $this->lastInsertId ();
	}
	
	public function addReply($arr) {
		$this->add ( $this->_comment_reply, $arr );
		return $this->lastInsertId ();
	}
	
	public function getReply($comment_id,$num) {
		
		$sql = "SELECT a.*,b.username,b.real_name,b.face,b.groupid FROM $this->_comment_reply a LEFT JOIN w_member_list b ON a.uid=b.uid WHERE comment_id=$comment_id LIMIT $num";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取一日之内的条数
	 * Enter description here .
	 *
	 *
	 * ..
	 */
	public function getCommentLimitTime($ip, $time) {
		$sql = "SELECT count(*) num FROM $this->_news_comment WHERE ip='$ip' AND `date`>'$time'";
		return $this->fetch ( $sql );
	}
	/**
	 * 删除评论
	 *
	 * @param array $where        	
	 */
	public function delComment($where) {
		return $this->delete ( $this->_news_comment, $where );
	}
	
	public function delReply($where) {
		return $this->delete ( $this->_comment_reply, $where );
	}
	/**
	 * 更新评论
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $v        	
	 * @param unknown_type $where        	
	 */
	public function saveCommentStatus($v, $where) {
		return $this->update ( $this->_news_comment, $v, $where );
	}
	/**
	 * 获取新闻分类
	 *
	 * @param int $fid
	 * 父类
	 * @return array $result
	 */
	public function getCategory($fid = NULL, $limit = 300, $type = NULL) {
		if ($fid == NULL) {
			$sql = "SELECT * FROM $this->_news_category  ORDER BY sort ASC LIMIT $limit";
		} else {
			$sql = "SELECT * FROM $this->_news_category WHERE `fid`=$fid   ORDER BY sort ASC LIMIT $limit";
		}
		
		if ($type == "ZTREE") {
			$sql = "SELECT * FROM $this->_news_category WHERE fid=0 ORDER BY sort ASC  LIMIT $limit";
		}
		
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取分类
	 * Enter description here ...
	 * @param unknown_type $where
	 */
	public function getCategoryByWhere($where) {
		return $this->getAll ( $this->_news_category, $where );
	}
	/**
	 *
	 * @param unknown_type $id        	
	 */
	public function getCateogryById($id) {
		return $this->getOne ( $this->_news_category, array ('id' => $id ) );
	}
	public function getHotCateGory() {
		$sql = "SELECT  count(cid) num,cid from w_news_list group by cid order by num desc LIMIT 15";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 添加分类
	 *
	 * @param array $arr        	
	 */
	public function addCategory($arr) {
		$this->add ( $this->_news_category, $arr );
		return $this->lastInsertId ();
	}
	/**
	 * 删除分类
	 *
	 * @param array $arr        	
	 */
	public function deleteCategory($arr) {
		$this->delete ( $this->_news_category, $arr );
	}
	/**
	 * 分类更名
	 *
	 * @param array $v        	
	 * @param array $where        	
	 */
	public function renameCategory($v, $where) {
		$this->update ( $this->_news_category, $v, $where );
	}
	/**
	 * 获取指定的分类
	 *
	 * @param unknown_type $categoryId        	
	 */
	public function getOneCategory($categoryId) {
		$sql = "SELECT * FROM $this->_news_category WHERE id=$categoryId";
		$rs = $this->fetch ( $sql );
		return $rs;
	}
	/**
	 * 获取一天分类信息
	 *
	 * @param array $where        	
	 * @return id fid
	 * name
	 * pic
	 */
	public function getOneCateInfo($where) {
		$result = $this->getOne ( $this->_news_category, $where );
		return $result;
	}
	/**
	 * 分类条数
	 *
	 * @package array $WHERE
	 */
	public function checkCateNum($WHERE) {
		$num = $this->getNum ( $this->_news_category, 'id', $WHERE );
		return $num;
	}
	/**
	 * 添加新闻
	 *
	 * @param array $arrValue        	
	 * @param string $content        	
	 * @return int $insertId
	 */
	public function addNews($arrValue, $content, $lang, $title, $iscontent = true) {
		$this->add ( $this->_news_list, $arrValue );
		$insertId = $this->lastInsertId ();
		$content = array ('nid' => $insertId, 'content' => $content, 'lang' => $lang, 'title' => $title );
		if ($iscontent) {
			$contentSql = $this->add ( $this->_news_content, $content );
		}
		return $insertId;
	}
	//添加文章
	public function addNewsContent($nid, $content, $lang, $title) {
		$content = array ('nid' => $nid, 'content' => $content, 'lang' => $lang, 'title' => $title );
		$contentSql = $this->add ( $this->_news_content, $content );
	}
	//添加图集
	public function addNewsImage($nid, $pic, $source, $remark) {
		$params = array ('nid' => $nid, 'image' => $pic, 'source' => $source, 'remark' => $remark );
		$contentSql = $this->add ( $this->_news_image, $params );
	}
	/**
	 * 提问者
	 */
	public function getMemberHasArticleNum() {
		$sql = "SELECT count(id) num,`author` FROM $this->_news_list GROUP BY author ORDER BY num DESC";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 更新新闻分类
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $v        	
	 * @param unknown_type $where        	
	 */
	public function saveNews($v, $where) {
		$this->update ( $this->_news_list, $v, $where );
	}
	/**
	 * 修改新闻
	 *
	 * @param int $id        	
	 * @param array $v        	
	 * @param text $content        	
	 */
	public function updateNews($v, $id, $content, $title, $lang) {
		$this->update ( $this->_news_list, $v, array ('id' => $id ) );
		return $this->update ( $this->_news_content, array ('content' => $content, 'title' => $title ), array ('nid' => $id, 'lang' => $lang ) );
	}
	/**
	 * 获取新闻列表
	 *
	 * @return -id -title
	 * -category
	 * -author
	 * -date
	 * -thumb
	 * -views
	 * -summary
	 * -remark
	 */
	public function getNewsList() {
		echo 'getNewsList已经弃用!';
		exit ();
		$sql = "SELECT * FROM $this->_news_list ORDER BY `id` DESC";
		$rs = $this->fetchAll ( $sql );
		return $rs;
	}
	/**
	 * 获取新闻
	 *
	 * @param unknown_type $where        	
	 * @param unknown_type $key        	
	 */
	public function getNewsByWhere($where, $key = NULL, $num = 10, $order = "id") {
		if (! empty ( $where ['cid'] )) {
			$sql = "SELECT a.id,keyword,subtitle,a.html,a.sort,a.summary,a.cid,a.views,a.thumb,a.pic,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE b.id=$where[cid] AND status=0 ORDER BY a.$order DESC LIMIT $num";
		} elseif (! empty ( $where ['title'] )) {
			$sql = "SELECT a.id,keyword,subtitle,a.html,a.sort,a.summary,a.cid,a.views,a.thumb,a.pic,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE a.title='$where[title]' AND status=0  ORDER BY a.$order DESC LIMIT $num";
		} else {
			$sql = "SELECT a.id,keyword,subtitle,a.html,a.sort,a.cid,a.summary,a.views,a.thumb,a.pic,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE  status=0  ORDER BY a.$order DESC LIMIT $num";
		}
		return $this->fetchAll ( $sql );
	}
	public function getNewsWhere($where, $key = NULL) {
		return $this->getOne ( $this->_news_list, $where, $key );
	}
	/**
	 *
	 *
	 *
	 *
	 * 获取新闻列表
	 *
	 * @param unknown_type $where        	
	 * @param unknown_type $key        	
	 * @param unknown_type $order_by        	
	 * @param unknown_type $limit        	
	 */
	public function getNewsListByWhere($where, $cid = null, $order_by = NULL, $limit = 20) {
		$where = $this->batchWhere ( $where );
		if (! empty ( $cid )) {
			$sql = "SELECT a.id,module,keyword,subtitle,a.html,a.sort,a.summary,a.cid,a.views,a.thumb,a.pic,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE $where AND a.cid IN ($cid) ORDER BY a.id DESC LIMIT $limit";
		} else {
			$sql = "SELECT a.id,module,keyword,subtitle,a.html,a.sort,a.summary,a.cid,a.views,a.thumb,a.pic,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE $where ORDER BY a.id DESC LIMIT $limit";
		}
		return $this->fetchAll ( $sql );
	}
	public function getRecycleNews($cid) {
		$sql = "SELECT id FROM $this->_news_list WHERE `cid`=$cid";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 通过flag标签调用
	 *
	 * @param unknown_type $num        	
	 * @param unknown_type $where        	
	 */
	public function getNewsByFlag($flag, $num, $cid, $order = "id") {
		if ($cid > 0) {
			$sql = "SELECT a.id,keyword,subtitle,a.html,a.sort,a.title,a.summary,a.thumb,a.views,a.pic,a.date,a.author,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE FIND_IN_SET($flag,flag) AND b.id IN ($cid) AND status=0 ORDER BY a.$order DESC LIMIT $num";
		} else {
			$sql = "SELECT a.id,keyword,subtitle,a.html,a.sort,a.title,a.summary,a.thumb,a.views,a.pic,a.date,a.author,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE FIND_IN_SET($flag,flag)  AND status=0  ORDER BY a.$order DESC LIMIT $num";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取最新的新闻
	 *
	 * @param int $limitNum        	
	 * @return -id -title
	 * -category
	 * -author
	 * -date
	 * -thumb
	 * -views
	 * -summary
	 * -remark
	 */
	public function getLastNews($cateid, $limitNum) {
		if (empty ( $cateid )) {
			$sql = "SELECT a.id,keyword,subtitle,a.summary,a.title,a.author,a.date,a.pic,b.name category FROM $this->_news_list a LEFT JOIN $this->_news_category b ON a.cid=b.id ORDER BY a.id DESC LIMIT $limitNum";
		} else {
			$sql = "SELECT * FROM $this->_news_list WHERE category=$cateid ORDER BY id DESC LIMIT $limitNum";
		}
		$result = $this->fetchAll ( $sql );
		return $result;
	}
	/**
	 * 查看新闻内容
	 *
	 * @param int $id        	
	 * @param String $cid        	
	 * @return array $rs
	 */
	public function getNewsContent($id = null, $limit = 1, $cid = null, $lang) {
		if (! empty ( $cid )) {
			/* 用于上下篇 */
			$sql = "SELECT id,status,keyword,subtitle,html,groupid,css,sort,cid,thumb,summary,pic,views,b.title,date,author,flag FROM $this->_news_list  WHERE  cid IN ($cid) AND id<$id ORDER BY id DESC LIMIT $limit";
			$rs ['pre'] = $this->fetchAll ( $sql );
			$sql = "SELECT id,status,keyword,subtitle,html,groupid,css,sort,cid,thumb,summary,pic,views,b.title,date,author,flag FROM $this->_news_list WHERE cid IN ($cid) AND id>$id ORDER BY id ASC LIMIT $limit";
			$rs ['after'] = $this->fetchAll ( $sql );
			return $rs;
		} else {
			$sql = "SELECT a.id,status,keyword,subtitle,groupid,a.css,a.sort,a.cid,a.thumb,a.pic,a.summary,a.views,b.title,a.date,a.author,a.flag,b.content FROM $this->_news_list a,$this->_news_content b WHERE a.id=b.nid AND a.id=$id AND b.lang=$lang";
			$rs = $this->fetch ( $sql );
			$updatesql = "UPDATE $this->_news_list SET `views`=`views`+1 WHERE `id`=$id";
			$this->exec ( $updatesql );
		}
		return $rs;
	}
	/**
	 * 获取图片集合
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function getNewsImage($id) {
		$sql = "SELECT * FROM $this->_news_image WHERE nid=$id";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 删除新闻
	 *
	 * @param int $id        	
	 */
	public function deleteNews($id) {
		$this->delete ( $this->_news_list, array ('id' => $id ) );
		$this->delete ( $this->_news_content, array ('nid' => $id ) );
	}
	/**
	 * 新闻总条数
	 *
	 * @param string $key
	 * 键名
	 * @param string $where
	 * 条件
	 */
	public function getNewsNum($key, $where = '') {
		if (empty ( $where )) {
			$sql = "SELECT count(id) num FROM $this->_news_list";
		} else {
			$sql = "SELECT count(id) num FROM $this->_news_list WHERE `cid` IN ($where)";
		}
		$num = $this->fetch ( $sql );
		return $num ['num'];
	}
	/**
	 * 获取评论总数
	 *
	 * @param unknown_type $key        	
	 * @param unknown_type $where        	
	 */
	public function getCommentNum($key, $cid = '') {
		if (empty ( $cid )) {
			$sql = "SELECT count(id) num FROM $this->_news_comment";
		} else {
			$sql = "SELECT count(id) num FROM $this->_news_comment WHERE `nid`=$cid";
		}
		$num = $this->fetch ( $sql );
		return $num ['num'];
	}
	/**
	 * 新闻分页
	 *
	 * @param int $start        	
	 * @param int $num        	
	 * @return array $rs
	 */
	public function newsPage($start, $num, $where, $order = 'id') {
		if (! empty ( $where )) {
			$sql = "SELECT a.id,module as mid,status,keyword,subtitle,a.html,a.css,a.sort,a.pic,a.thumb,a.summary,a.cid,a.views,a.title,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE a.cid IN ($where) AND status=0  ORDER BY a.$order DESC LIMIT $start,$num";
		} else {
			$sql = "SELECT  a.id,status,module as mid,keyword,subtitle,a.html,a.css,a.sort,a.pic,a.cid,a.title,a.views,a.summary,a.thumb,a.date,a.author,a.flag,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id  WHERE b.id!=1067  AND status=0  ORDER BY a.$order DESC LIMIT $start,$num";
		}
		return $this->fetchAll ( $sql );
	}
	/*
	 * 搜索标题
	 */
	public function getOneNewsLikeTitle($key, $value, $cid = NULL, $limit = 20) {
		if (! empty ( $cid )) {
			$sql = "SELECT  a.id,module,status,keyword,subtitle,a.cid,a.title,a.date,a.pic,a.author,a.views,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE $key like '%$value%'  AND  a.cid IN ($cid) LIMIT $limit";
		} else {
			// 考虑下属分类的情况
			$sql = "SELECT  a.id,status,module,keyword,subtitle,a.cid,a.title,a.date,a.pic,a.author,a.views,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE $key like '%$value%' LIMIT $limit";
		}
		return $this->fetchAll ( $sql );
	}
	public function getTempLikeName($name, $limit) {
		// 考虑下属分类的情况
		$sql = "SELECT  id,name,keyword,subtitle,remark FROM $this->_templates WHERE `name` like '%$name%' LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	public function getAllTemp($where, $key, $limit) {
		return $this->getAll ( $this->_templates, $where, $key, "id DESC", $limit );
	}
	public function getOneNewsLikeId($id, $cid = NULL, $limit = 20) {
		if (! empty ( $cid )) {
			$sql = "SELECT  a.id,keyword,subtitle,a.cid,a.title,a.date,a.pic,a.author,a.views,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE a.`id` like '%$id%'  AND  a.cid IN ($cid) LIMIT $limit";
		} else {
			// 考虑下属分类的情况
			$sql = "SELECT  a.id,keyword,subtitle,a.cid,a.title,a.date,a.pic,a.author,a.views,a.flag,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE a.`id` like '%$id%' LIMIT $limit";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 保存模板
	 *
	 * @param unknown_type $source        	
	 * @param unknown_type $id        	
	 */
	public function saveTempSource($source, $id) {
		$sql = "UPDATE $this->_templates SET `source`='$source' WHERE `id`='$id'";
		return $error = $this->exec ( $sql );
	
		// $this->update ( $this->_templates, array ('source' => $source ),
	// array ('id' => $id ) );
	}
	/**
	 * 绑定模板
	 *
	 * @param array $v        	
	 * @param int $id        	
	 */
	public function bindCategoryById($v, $id) {
		$this->update ( $this->_news_category, $v, array ('id' => $id ) );
	}
	/**
	 * 获取多个分类中的新闻
	 *
	 * @param unknown_type $ids        	
	 */
	public function getNewsListByIds($ids, $limitNum, $order = "id") {
		$sql = "SELECT *,keyword,subtitle,a.id as id,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE `cid` IN ($ids) ORDER BY a.$order DESC LIMIT $limitNum  ";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 根据新闻ids获取
	 *
	 * @param unknown_type $ids        	
	 */
	public function getNewsListInIds($ids, $limitNum, $order = "id") {
		$sql = "SELECT a.*,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE  a.id IN ($ids) ORDER BY a.$order DESC LIMIT $limitNum  ";
		return $this->fetchAll ( $sql );
	}
	public function getNewsIdByIds($ids) {
		$sql = "SELECT id,date,html  FROM w_news_list WHERE cid IN ($ids) ORDER BY id DESC";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 获取多个分类中的新闻
	 *
	 * @param unknown_type $ids        	
	 */
	public function getNewsListByFid($flag, $ids, $limitNum) {
		$sql = "SELECT *,a.id as id,b.id as category_id,b.name as category_name FROM w_news_list a LEFT JOIN w_news_category b ON a.cid=b.id WHERE FIND_IN_SET($flag,flag) AND `cid` IN ($ids) ORDER BY a.id DESC LIMIT $limitNum  ";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 分类树
	 *
	 * @param unknown_type $v        	
	 * @param unknown_type $where        	
	 */
	public function saveCateTopId($v, $where) {
		$this->update ( $this->_news_category, $v, $where );
	}
	/**
	 * 获取分类洛湛字段
	 *
	 * @return array
	 */
	public function getCateExtendByCid($cid) {
		return $this->getAll ( $this->_cate_extend, array ('cid' => $cid ) );
	}
	/**
	 * 生成新的配置
	 *
	 * @param unknown_type $params        	
	 */
	public function addCateExtend($params) {
		$this->add ( $this->_cate_extend, $params );
	}
	/**
	 * 删除原有配置 重新生成
	 *
	 * @param unknown_type $cid        	
	 */
	public function delCateExtend($cid) {
		$this->delete ( $this->_cate_extend, array ('cid' => $cid ) );
	}
	/**
	 * 获取内容管理列名
	 */
	public function getColumName() {
		$sql = "show columns from w_news_list";
		return $this->fetchAll ( $sql );
	}
	public function getCategoryByZtree($ids) {
		if (empty ( $ids )) {
			$sql = "SELECT `id`,`fid` as pid ,`name` FROM $this->_news_category";
		} else {
			$sql = "SELECT `id`,`fid` as pid ,`name` FROM $this->_news_category WHERE id IN ($ids)";
		}
		return $this->fetchAll ( $sql );
	}
	/**
	 * 更新分类字段绑定
	 *
	 * @param unknown_type $v        	
	 * @param unknown_type $where        	
	 */
	public function saveCategoryKeys($v, $where) {
		$this->update ( $this->_news_category, $v, $where );
	}
	/**
	 * 根据用户来查询
	 *
	 * @return mixed
	 */
	public function countNewsByAuthor($author) {
		$sql = "SELECT author,count(id) num FROM $this->_news_list WHERE author IN ($author) GROUP BY author ORDER BY num DESC";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 统计今日添加数量
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $today        	
	 */
	public function countNewsTodayByAuthor($today, $author) {
		$sql = "SELECT author,count(id) num FROM $this->_news_list WHERE date>$today AND author in ($author) GROUP BY author ORDER BY num DESC";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 统计昨天添加数量多少
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $today        	
	 */
	public function countNewsYesterdayByAuthor($yesterday, $today, $author) {
		$sql = "SELECT author,count(id) num FROM $this->_news_list WHERE date>$yesterday AND date<$today AND author in ($author) GROUP BY author ORDER BY num DESC";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 查找评论状态
	 * Enter description here .
	 *
	 *
	 * ..
	 *
	 * @param unknown_type $type        	
	 * @param unknown_type $where        	
	 */
	public function findCommentByWhere($where, $num = 20) {
		return $this->getAll ( $this->_news_comment, $where, null, "id DESC", $num );
	}
	public function findCommentById($nid, $status, $time, $limit) {
		$sql = "SELECT * FROM $this->_news_comment WHERE nid=$nid AND `status`=$status AND `date`<=$time  ORDER BY id DESC LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	/**
	 * 返回NewsModel
	 *
	 * @return NewsModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}